package org.ggb.service.impl;


import org.ggb.pojo.Order;
import org.ggb.pojo.Product;
import org.ggb.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    /**
     * 根据主键查询订单id
     *
     * @param id
     * @return
     */
    @Override
    public Order selectOrderById(Integer id) {
        return new Order(1,"order-001","中国",324164D,selectProductByLoadBalancerClient());
    }

    /**
     * 通过discoveryClient方式实现
     * @return
     */

    private List<Product> selectProductByDiscoveryClient() {
        StringBuffer sb=null;
        // 获取服务器列表
        List<String> serviceIds=discoveryClient.getServices();
        if(CollectionUtils.isEmpty(serviceIds)){
            return null;
        }
        //根据服务器名称获取服务器
        List<ServiceInstance> serviceInstances=discoveryClient.getInstances("service-provider");
        if(CollectionUtils.isEmpty(serviceInstances)){
            return null;
        }

        ServiceInstance si=serviceInstances.get(0);
        sb=new StringBuffer();
        sb.append("http://"+si.getHost()+":"+si.getPort()+"/product/list");
        System.out.println(sb.toString());
        //封装返回
        ResponseEntity<List<Product>> response=restTemplate.exchange(
                sb.toString(),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {}
        );
        return response.getBody();
    }

    private List<Product>  selectProductByLoadBalancerClient(){
        StringBuffer sb=null;
        // 获取服务器列表
        ServiceInstance si=loadBalancerClient.choose("service-provider");
        if(si==null){
            return null;
        }
        sb=new StringBuffer();
        sb.append("http://"+si.getHost()+":"+si.getPort()+"/product/list");
        //封装返回
        ResponseEntity<List<Product>> response=restTemplate.exchange(
                sb.toString(),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {}
        );
        return response.getBody();
    }

    private List<Product>  selectProductByLoadBalancerAnnotation(){
        //封装返回
        ResponseEntity<List<Product>> response=restTemplate.exchange(
                "http://service-provider/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {}
        );
        return response.getBody();
    }
}
